package com.cby.widget.sample.fragments

import android.os.Bundle
import android.util.DisplayMetrics
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.cby.widget.lib.html.*
import com.cby.widget.sample.R

/**
 * @description: HtmlTextViewFragment
 *
 * @author ChenBaoyang
 * @date 2021/11/9 17:00
 */
class HtmlTextViewFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_html_view, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val textView: HtmlTextView = view.findViewById(R.id.html_text)

        //text.setRemoveFromHtmlSpace(false); // default is true

        //text.setRemoveFromHtmlSpace(false); // default is true
        textView.setClickableTableSpan(ClickableTableSpanImpl())
        val drawTableLinkSpan = DrawTableLinkSpan()
        drawTableLinkSpan.setTableLinkText("[tap for table]")
        textView.setDrawTableLinkSpan(drawTableLinkSpan)

        // Best to use indentation that matches screen density.

        // Best to use indentation that matches screen density.
        val metrics = DisplayMetrics()
        activity!!.windowManager.defaultDisplay.getMetrics(metrics)
        textView.setListIndentPx(metrics.density * 10)

        // a tag click listener

        // a tag click listener
        textView.setOnClickATagListener { widget, spannedText, href ->
            val toast = Toast.makeText(activity, null, Toast.LENGTH_SHORT)
            toast.setText(href)
            toast.show()
            false
        }
        textView.blockQuoteBackgroundColor = resources.getColor(R.color.whitish)
        textView.blockQuoteStripColor = resources.getColor(R.color.blue)

        textView.setHtml(R.raw.example, HtmlResImageGetter(context!!))
    }

    inner class ClickableTableSpanImpl : ClickableTableSpan() {
        override fun onClick(widget: View) {

        }

        override fun newInstance(): ClickableTableSpan {
            return ClickableTableSpanImpl()
        }


    }
}